package com.paobuji.aidebao.myapplication.ui;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.activity.BaseActivity;
import com.paobuji.aidebao.myapplication.appinterface.GetInterfaceObjctListener;
import com.paobuji.aidebao.myapplication.bean.AddSportHistory;
import com.paobuji.aidebao.myapplication.bean.GetSportHistoryList;
import com.paobuji.aidebao.myapplication.bean.UserBeanResult;
import com.paobuji.aidebao.myapplication.tool.ACache;
import com.paobuji.aidebao.myapplication.tool.GetAppInfoUtil;
import com.paobuji.aidebao.myapplication.tool.MyIntentTools;
import com.paobuji.aidebao.myapplication.view.CustomTitlebar;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FinishSportActivity extends BaseActivity {

    public double sportDis = 0;   //运动里程
    public int sportTime = 0;     //运动时间
    public int sportCor = 0;      //消耗卡路里
    Intent intent;
    @BindView(R.id.mainTitle)
    CustomTitlebar mainTitle;
    @BindView(R.id.timeData)
    TextView timeData;
    @BindView(R.id.corData)
    TextView corData;
    @BindView(R.id.disData)
    TextView disData;
    @BindView(R.id.commit)
    Button commit;
    ACache aCache;
    MyIntentTools myIntentTools;

    Context context;

    UserBeanResult.DataBean.UserBean userBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finish_sport);
        ButterKnife.bind(this);
        context = getApplicationContext();
        //设置全透明
        StatusBarUtil.setTransparent(this);

        intent = getIntent();
        aCache = ACache.get(context);
        myIntentTools = new MyIntentTools();

        //在缓存中获取用户数据
        userBean = (UserBeanResult.DataBean.UserBean) aCache.getAsObject("userBean");
        //判断用户数据是否拿到，若没有的话从服务器拉取
        if (userBean == null) {
            userBean = new UserBeanResult.DataBean.UserBean();
        }

        sportDis = intent.getDoubleExtra("sportDis", 0.0);
        sportTime = Integer.parseInt(intent.getStringExtra("sportTime"));
        sportCor = intent.getIntExtra("sportCor", 0);


        //Log.i("时间",sportTime + "    sportTime/60 = " + sportTime/60 + "   (int)(sportTime%60) =  " + (int)(sportTime%60));
        timeData.setText((sportTime / 60 + ":" + (int) (sportTime % 60)) + "S");
        corData.setText(sportCor + "KCal");
        disData.setText(sportDis + getString(R.string.unit));

        //标题栏事件监测
        mainTitle.setAction(new CustomTitlebar.TitleBarOnClickListener() {
            @Override
            public void performAction(View view) {
                switch (view.getId()) {
                    case R.id.iv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "左边图片按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.iv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @OnClick(R.id.commit)
    public void onClick() {
        Date date = new Date();

        String time = date.toLocaleString();

        Log.i("md", "时间time为： " + time);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String sim = dateFormat.format(date);

        Gson gson = new Gson();
        ArrayList<GetSportHistoryList.DataBean.SporthitorylistBean> sporthitorylist = (ArrayList<GetSportHistoryList.DataBean.SporthitorylistBean>) aCache.getAsObject("sporthitorylist");
        if (sporthitorylist == null) {
            sporthitorylist = new ArrayList<GetSportHistoryList.DataBean.SporthitorylistBean>();
        }
        GetSportHistoryList.DataBean.SporthitorylistBean sporthitorylistBean = new GetSportHistoryList.DataBean.SporthitorylistBean();
        sporthitorylistBean.setAllCor(sportCor);
        sporthitorylistBean.setAlldis(sportDis);
        sporthitorylistBean.setSportDate(sim);
        sporthitorylistBean.setSportTime(sportTime);
        sporthitorylist.add(sporthitorylistBean);
        aCache.put("sporthitorylist", sporthitorylist);
        double sportDisAll = aCache.getAsString("sportDisAll") == null ? 0 : Double.parseDouble(aCache.getAsString("sportDisAll"));
        int sportTimeAll = aCache.getAsString("sportTimeAll") == null ? 0 : Integer.parseInt(aCache.getAsString("sportTimeAll"));
        int sportCorAll = aCache.getAsString("sportCorAll") == null ? 0 :  Integer.parseInt(aCache.getAsString("sportCorAll"));
        aCache.put("sportDisAll", String.valueOf(sportDisAll + sportDis));
        aCache.put("sportTimeAll", String.valueOf(sportTimeAll+ sportTime));
        aCache.put("sportCorAll", String.valueOf( sportCorAll+ sportCor));


        //运动里程
        //运动时间
        //消耗卡路里
        finish();
//        myIntentTools.addSportHistory(context,userBean.getId(),sportTime,sportDis,sportCor);
//        myIntentTools.setGetInterfaceObjctListener(new GetInterfaceObjctListener() {
//            @Override
//            public void getObjectDate(Object data) {
//                if(((AddSportHistory)data).getCode() == 0){
//                    finish();
//                }else{
//                    toastLog(R.string.erroneous + "");
//                }
//            }
//        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        //设置为竖屏
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        }
    }
}
